package com.syn.syn1;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: zch
 * @Date: 2019/1/7 14:26
 * @Description:重入锁
 */
public class ReenterLock  implements Runnable{

    public static int i=0;
    public static ReentrantLock lock=new ReentrantLock();

    public static void main(String[] args) throws InterruptedException
    {
        ReenterLock li = new ReenterLock();
        Thread t1 = new Thread(li);
        Thread t2 = new Thread(li);
        t1.start();
        t1.join();
        t2.join();
        System.out.println(i);
    }

    @Override
    public void run() {
        for (int j=0;j<1000000;j++) {
            lock.lock();
            try {
                i++;
            }
            finally {
                lock.unlock();
            }
        }

    }
}
